//
//  YJThirdSharePageView.m
//  Sports-Go
//
//  Created by rimi on 16/10/6.
//  Copyright © 2016年 YRL. All rights reserved.
//

#import "YJThirdSharePageView.h"
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKUI/ShareSDK+SSUI.h>
#import "WXApi.h"
#import "WeiboSDK.h"
@implementation YJThirdSharePageView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self addUI];
    }
    return self;
}
- (void)addUI{

    UIView *view = [[UIView alloc] initWithFrame:RzCGRectMake(10, 357, 355, 220)];
    view.backgroundColor = getColor(236, 236, 236, 1);
    view.layer.cornerRadius = 20*bi;
    view.layer.masksToBounds = YES;
    
    [self addSubview:view];
   
    UIButton *backButton = [[UIButton alloc] initWithFrame:RzCGRectMake(10, 597, 355, 50)];
    [backButton setTitle:@"取消" forState:UIControlStateNormal];
    [backButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    backButton.layer.cornerRadius = 10*bi;
    backButton.layer.masksToBounds = YES;
    backButton.backgroundColor = getColor(236, 236, 236, 1);
    [backButton addTarget:self action:@selector(backTheLastPage) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:backButton];
     NSArray *icon = @[@"share1",@"share2",@"share3",@"share4",@"share5",@"share6"];
    for (int i = 0; i < 6; i ++) {
        UIButton *shareButton = [[UIButton alloc] initWithFrame:RzCGRectMake((35 + (i % 4)*75), (30 + (i / 4)*90), 50, 50)];
        shareButton.backgroundColor = [UIColor whiteColor];
        shareButton.tag = 700 + i;
        
        shareButton.layer.cornerRadius = 10*bi;
        shareButton.layer.masksToBounds = YES;
        
        [shareButton setImage:[UIImage imageNamed:icon[i]] forState:UIControlStateNormal];
        [shareButton addTarget:self action:@selector(clickTheShareButon:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:shareButton];
    }
    NSArray *titleArray = @[@"QQ",@"QQ空间",@"新浪微博",@"微信好友",@"微信朋友圈",@"微信收藏"];
    for (int i = 0; i < 6; i ++) {
        UILabel *title = [[UILabel alloc] initWithFrame:RzCGRectMake((15 + (i % 4)*75), (90 + (i / 4)*90), 90, 15)];
        title.text = titleArray[i];;
        title.textAlignment = NSTextAlignmentCenter;
        title.font = [UIFont systemFontOfSize:12];
        [view addSubview:title];
    }

  
}

- (void)clickTheShareButon:(UIButton *)sender{
    
    switch (sender.tag-700) {
        case 0:
            //qq分享
            [self addSharePageWithIndex:1];
            break;
        case 1:
            //qq空间分享
            [self addSharePageWithIndex:2];
            break;
        case 2:
            //微博分享
            [self addSharePage];
            break;
        case 3:
            //微信好友分享
            [self addSharePageWithIndex:3];
            break;
        case 4:
            //微信朋友圈分享
            [self addSharePageWithIndex:4];
            break;
        case 5:
            //微信收藏分享
            [self addSharePageWithIndex:5];
            break;
        default:
            break;
    }
}
- (void)backTheLastPage{

    
    [UIView animateWithDuration:0.5 animations:^{
        self.frame = RzCGRectMake(0, 667, 375, 667);
    }];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"openTheTabBar" object:nil];
    
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [UIView animateWithDuration:0.5 animations:^{
        self.frame = RzCGRectMake(0, 667, 375, 667);
    }];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"openTheTabBar" object:nil];
    
}
- (void)addSharePage{
    
    //1、创建分享参数
    NSArray* imageArray = @[[UIImage imageNamed:@"icon.png"]];
    //（注意：图片必须要在Xcode左边目录里面，名称必须要传正确，如果要分享网络图片，可以这样传iamge参数 images:@[@"http://mob.com/Assets/images/logo.png?v=20150320"]）
    if (imageArray) {
        
        NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
        [shareParams SSDKEnableUseClientShare];//允许客户端进行分享
        
        [shareParams SSDKSetupSinaWeiboShareParamsByText:@"SportsGo让我们一起来运动~" title:@"SportsGo" image:imageArray url:[NSURL URLWithString:@"http://mob.com"] latitude:0 longitude:0 objectID:nil type:SSDKContentTypeAuto];
        
        
        //直接跳转到第三方平台进行分享
        
        [ShareSDK share:SSDKPlatformTypeSinaWeibo parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
            switch (state) {
                case SSDKResponseStateSuccess:
                {
                    NSLog(@"分享成功");
                    break;
                }
                case SSDKResponseStateFail:
                {
                    NSLog(@"分享失败");
                    break;
                }
                default:
                    break;
            }
        }];
    }
}
- (void)addSharePageWithIndex:(NSInteger)index{
    
    //创建分享参数
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    //1、创建分享参数
    NSArray* imageArray = @[[UIImage imageNamed:@"icon.png"]];
    [shareParams SSDKSetupShareParamsByText:@"分享内容"
                                     images:imageArray //传入要分享的图片
                                        url:[NSURL URLWithString:@"http://mob.com"]
                                      title:@"分享标题"
                                       type:SSDKContentTypeAuto];
    
    if (index == 1) {
        //qq分享
        //进行分享
        [ShareSDK share:SSDKPlatformTypeQQ //传入分享的平台类型
             parameters:shareParams
         onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
             
         }];
        
    }else if (index == 2){
        //qq空间
        [ShareSDK share:SSDKPlatformSubTypeQZone //传入分享的平台类型
             parameters:shareParams
         onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
             
         }];
        
        
    }else if (index == 3){
        //微信好友分享
        [ShareSDK share:SSDKPlatformSubTypeWechatSession //传入分享的平台类型
             parameters:shareParams
         onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
             
         }];
        
        
    }else if (index == 4){
        //微信朋友圈
        [ShareSDK share:SSDKPlatformSubTypeWechatTimeline //传入分享的平台类型
             parameters:shareParams
         onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
             
         }];
    }else if (index == 5){
        //微信收藏
        
        [ShareSDK share:SSDKPlatformSubTypeWechatFav //传入分享的平台类型
             parameters:shareParams
         onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
             
         }];
        
    }
    
}

@end
